/**
 * @Author：ming
 * @Date：2023/10/6 8:48 PM
 * @Filename：Lc146
 */

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author ming
 */
public class Lc146 {
}
class LRUCache {
    int capacity;
    Map<Integer,Integer> map;

    public LRUCache(int capacity) {
        this.capacity = capacity;
      map =  new LinkedHashMap<Integer,Integer>();
    }

    public int get(int key) {
        if(map.containsKey(key)){
            int value = map.get(key);
            map.remove(key);
            map.put(key,value);
            return map.get(key);
        }else {
            return -1;
        }
    }

    public void put(int key, int value) {
        if(map.containsKey(key)){
            map.remove(key);
            map.put(key,value);
        }else {
            if(capacity==0){
                Iterator<Integer> iterator = map.keySet().iterator();
                Integer next = iterator.next();
                Integer remove = map.remove(next);
                map.put(key,value);
            }else {
                capacity--;
                map.put(key,value);
            }
        }
    }
}
